package com.dingwei.lesyndrome.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.app.MyApplication;
import com.dingwei.lesyndrome.bean.PersonalInfoBean;
import com.dingwei.lesyndrome.mvp.presenter.PersonalInfoPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/26.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 个人信息
 */

public class PersonalInfoActivity extends BaseActivity implements Contract.PersonalInfoView {
    PersonalInfoPresenter personalInfoPresenter = new PersonalInfoPresenter(this);
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.ll_img_head)
    LinearLayout llImgHead;
    @Bind(R.id.username)
    SuperTextView username;
    @Bind(R.id.gender)
    SuperTextView gender;
    @Bind(R.id.password)
    SuperTextView password;
    @Bind(R.id.data_layout)
    ScrollView dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;
    private String lastPath;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_personal_info;
    }

    @Override
    protected void initData() {
        personalInfoPresenter.initData();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.ll_img_head, R.id.username, R.id.gender, R.id.password,R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_img_head:
                PictureSelector.create(PersonalInfoActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .theme(R.style.picture_white_style)
                        .imageSpanCount(3)
                        .selectionMode(PictureConfig.SINGLE)
                        .previewImage(true)
                        .isCamera(true)
                        .isZoomAnim(true)
                        .enableCrop(true)// 是否裁剪 true or false
                        .compress(true)// 是否压缩 true or false
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽 true or false
                        .circleDimmedLayer(true)// 是否圆形裁剪 true or false
                        .showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                        .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                        .rotateEnabled(true) // 裁剪是否可旋转图片 true or false
                        .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.username:
                intent = new Intent(BaseActivity.context, ModifyNameActivity.class);
                intent.putExtra("nickname", username.getRightString().toString());
                startActivityForResult(intent, 0);
                break;
            case R.id.gender:
                personalInfoPresenter.showChooseSexDialog(this,gender.getRightString());
                break;
            case R.id.password:
                intent = new Intent(BaseActivity.context, ModifyPasswordActivity.class);
                startActivityForResult(intent,5);
                break;
            case R.id.no_net_btn:
                personalInfoPresenter.initData();
                break;
        }
    }

    @Override
    public void setData(PersonalInfoBean bean) {
        Glide.with(BaseActivity.context)
                .load(bean.data.showHportrait)
                .apply(MyApplication.headOptions)
                .into(imgHead);

        username.setRightString(bean.data.nickname);
        switch (bean.data.sex) {
            case 0:
                gender.setRightString("保密");
                break;
            case 1:
                gender.setRightString("男");
                break;
            case 2:
                gender.setRightString("女");
                break;
        }

        showData("success");

    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void uploadPicSuccess() {

        Glide.with(BaseActivity.context)
                .load(new File(lastPath))
                .apply(MyApplication.headOptions)
                .into(imgHead);
    }

    @Override
    public void ChooseSex(String genderItem) {
        personalInfoPresenter.saveGender(genderItem);
    }

    @Override
    public void saveGenderSuccess(String genderItem) {
        gender.setRightString(genderItem);
    }

    public void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 0:
                username.setRightString(data.getStringExtra("nickname"));
                break;
            case PictureConfig.CHOOSE_REQUEST:
                // 图片选择结果回调
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                // 例如 LocalMedia 里面返回三种path
                // 1.media.getPath(); 为原图path
                // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                lastPath = selectList.get(0).getCompressPath();
                personalInfoPresenter.uploadHeadImg(lastPath);
                break;
            case 5:
                intent = new Intent();
                intent.putExtra("id","1");
                setResult(Activity.RESULT_OK, intent);
                finish();
                break;
        }
    }
}
